package com.wxmit.framework.satoken.core.util;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.security.*;

/**
 * RsaUtil
 *  用于密码加解密
 *
 * @author huangxiong
 * @version V1.0
 * @date 2022/12/26 15:04
 */
public class RsaUtil {

    /**
     *  公钥
     */
    public static final String PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApuSWYDwc9pvrt0JnH4iTJS51PuT4hniljOHQLeSHmgMdNW5" +
            "15QhcxqDMIGeu6nJ289D9SOuylQC9lNohzPYg/QTRU0Ipy2TQuSDahOlf6KpgGuw1fukt6jylNumrq4i+lfUqEtgcxHriQs374DJSM75WnZRI9jqmz3/ZM6PSret" +
            "2xCFFGon+qALDA9JMqBSZ8u28wE/oiAUzhB64YmyxxoTSEzxSMHpcvck4SERnFeybhjR8A8SDuv1iaw7IU1D5DRoXEVEv0ZDZwmM97O3guFt+X62ItDQ" +
            "tHMDhIqXUGMNqeQuDG6nIMfs668FmHzuK1PjczfE0FyR+htK1+m+BuwIDAQAB";
    /**
     *  私钥
     */
    private static final String  PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCm5JZgPBz2m+u3QmcfiJMlLnU+5P" +
            "iGeKWM4dAt5IeaAx01bnXlCFzGoMwgZ67qcnbz0P1I67KVAL2U2iHM9iD9BNFTQinLZNC5INqE6V/oqmAa7DV+6S3qPKU26auriL6V9SoS2B" +
            "zEeuJCzfvgMlIzvladlEj2OqbPf9kzo9Kt63bEIUUaif6oAsMD0kyoFJny7bzAT+iIBTOEHrhibLHGhNITPFIwely9yThIRGcV7JuGNHwDxIO6" +
            "/WJrDshTUPkNGhcRUS/RkNnCYz3s7eC4W35frYi0NC0cwOEipdQYw2p5C4Mbqcgx+zrrwWYfO4rU+NzN8TQXJH6G0rX6b4G7AgMBAAECggEACz7" +
            "b4yAp03QuLesLh9RsFdVLkTFcjbMTdKrCnFUJzhbnoPIQf55QohGzr3tNDf8ohMVQS9mV7vRd0eng3S64ilQIxtNufYYNimvsTYhsLgFM+INyF1se/" +
            "OmZOFoGd0h/jxs1+o8VYSfmlxnKHxFE+LeanoOfZvgMxW6X9CLVz2IyEvyusmWahZS/EF0OYNUl9FR/u3LlcRIMnVbO19VjKpK4QAPiVEeW5Z0uj0N/" +
            "FKzbQH7uBSuZiRnikShBS/ZCp3NoTVn/wg0FqI3Mb4GCDzbA04m+N3tW8ZEMjKKyiDgg0mmreK+FbmpusgUAY/+9s2YL3DoFlFEUjMgk1b0sAQKBgQD2zX" +
            "Is3ms/nhAoTVwAoIA4Uxsrq/bJB75zLvDNbDa3MgpZHwq1pB42FeThbg24tYeUK44s9PII2EX2a1oc6RRavGGCru5UmYbXwDqEefW7oUWuN/CIPqCf6UieiyT" +
            "lW8rbIwgEGfWSQ1UB0FsxjCNI7ECan6AHlq2wRhr76OkKEwKBgQCtHMj30w8oIS2yiE9p1UnI9Xo3HIsoi+Zr9l9MbouaHlcP+9aTuilqhzEVNBC/hqi+O3NhXvX" +
            "NKfBAsSpTDfj9DoO5cRfkuSc9+Zxgj9eXIqHeZjKyeaj55vLN20HoikQYABJ1mEFOKvJy0ejZJ34wGpbpAXRQxaSrIV+EZOMeuQKBgQDqvJr0vBOrrW3Wt0VjP0T" +
            "UzY6RKf0OVXQ535Ke/2bHNSwMee7YtqEZdhd8pg0c+mNadVbDwbGW4gUhDBfoWXPHnVbtUe5Svrc265l8uPcVH53Dx2baXx6tGde9G8Da+URmytnr9LjSBYKLuB2" +
            "UcaJ50GLd7ibqMNFAUF2H7Y4AywKBgBTATpbmhfqZf5DXJi3Zk26zfmL82PGTU5u9fM78sV9YI7eWyLIMrspGSmvwQdGzoqarGbmiWHSvgk0+74PlOOOcyc2A8QxJ" +
            "5ZpYQZaRp5ZGYID4Xztlugu604A5uDzX1jHKUGwoRCvKmiWJZBAj28yIY26q7faAuqyI45pLPa6BAoGAaTfPWLOekBcZf9AETFTobgosjGTkSz3DPwBTZ1nFF3iGo" +
            "Hd8/flHLL4+28i4xke7QjTJjgY/HHSUvMotlyHtA2m5EN+hcG/b6UdzQQseKMT1QdiXZuMRnxQVJ0C02Ku58xgTpBWPrfUhX2qtd3QDQeRpcCw0nlKYFCH36N6BmiM=";

    /**
     * 私钥解密
     *
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String text)
    {
        return decryptByPrivateKey(PRIVATE_KEY, text);
    }



    /**
     * 私钥解密
     *
     * @param privateKeyString 私钥
     * @param text 待解密的文本
     * @return 解密后的文本
     */
    public static String decryptByPrivateKey(String privateKeyString, String text)
    {
        RSA rsa = new RSA(privateKeyString, null);
        return rsa.decryptStr(text, KeyType.PrivateKey);
    }



    /**
     * 公钥加密
     *
     * @param publicKeyString 公钥
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String publicKeyString, String text)
    {
        RSA rsa = new RSA(null, publicKeyString);
        return rsa.encryptHex(text, KeyType.PublicKey);

    }

    /**
     * 公钥加密
     *
     * @param text 待加密的文本
     * @return 加密后的文本
     */
    public static String encryptByPublicKey(String text)
    {
        return encryptByPublicKey(PUBLIC_KEY,text);

    }

    public KeyPair genKey() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
       return keyPairGenerator.generateKeyPair();
       //
//        PublicKey rsaPublicKey = keyPair.getPublic();
//        PrivateKey rsaPrivateKey =  keyPair.getPrivate();
//        String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
//        String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
    }
}
